<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Manage_users extends CI_Controller {
    public function index()
    {
        $data['title'] = "Manage Users";
        $user_response = $this->session->flashdata('user_response');
        $data['data'] = array (
            "response" => $user_response
                    );
        
        $data['js'] = array("manage_users.js");
        
        $data['view'] = "settings/view_manage_users";
        $this->load->view('view_template',$data);
    }
        
    public function searchResults(){
        extract($this->ajax->post("query","filter"));
        $this->load->model('users');
        $results = array();
        $options = array();
        switch($filter){
            case 0://Full Name
                $results = $this->users->getLikeName($query);
                break;
            case 1://Email
                $results = $this->users->getLikeEmail($query);
                break;
            case 2://Username
                $results = $this->users->getLikeUsername($query);
                break;
            default://We didn't get a good input
                return;
        }
        
        //Get data in format for view_options
        foreach($results->result() as $result){
            $options[$result->user_id] = array(
                'title'=> $result->user_title,
                'desc' => $result->user_login.' - '.$result->user_email
            );
        }
        
        $this->load->view('ajax/view_list',
            array(
                "options"=>$options,
                "function"=>"selectUser"
            )
        );
    } 
    
    public function previewUser(){
        extract($this->ajax->post("user_id"));
        $this->load->model('users');
        $row = $this->users->getByID($user_id);
        $basetypes = $this->users->getBasetypes($user_id);
                
        $this->load->view('ajax/view_preview_user',array(
            "user"=>$row,
            "basetypes"=>$basetypes
            ));
    }
    
    public function deleteUser(){
        extract($this->ajax->post("user_id"));
        $this->load->model('users');
                
        if ($this->users->deleteUser($user_id)){
            $this->session->set_flashdata('user_response','Delete successful.');
            echo "good";
        }
        else{
            $this->session->set_flashdata('user_response','An error occured when deleting the user.');
            echo "An error occurred";
        }
    }
}